package com.olive.service.impl;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.olive.constants.UserConstants;
import com.olive.service.SmsCodeSendService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SmsCodeSendServiceImpl implements SmsCodeSendService {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	public boolean sendSmsCode(String mobile, String code) {
		//因为这里是示例所以就没有真正的使用第三方发送短信平台。
		String sendCode = String.format("你好你的验证码%s，请勿泄露他人。", code);
		log.info("向手机号" + mobile + "发送的短信为:" + sendCode);
		//存入redis,以手机号_SMS_key为key，值是验证码，5分钟过期
		stringRedisTemplate.opsForValue().set(mobile + "_" + UserConstants.SMS_REDIS_KEY, code, Duration.ofMinutes(5));
		return true;
	}
}
